<style lang="scss">
	@import '@/common/uni-ui.scss';

	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #efeff4;
		min-height: 100%;
		height: auto;
	}
	.content {
		padding-top: 210rpx;
	}
	.list-picture {
		width: 100%;
		height: 145px;
	}

	.thumb-image {
		width: 100%;
		height: 100%;
	}
	.top-bar {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		background-color: #FFFFFF;
		width: 100%;
		.top-menu{
			display: flex;
			flex-direction: row;
			flex-wrap: nowrap;
			padding: 16rpx;
			.menu-item {
				display: flex;
				flex-direction: column;
				flex-wrap: nowrap;
				align-items: center;
				color: $uni-text-color-grey;
				margin: 0 16rpx;
				&.active{
					color: $uni-text-color;
					&::after{
						content: '';
						width: 40rpx;
						border: 1px solid $uni-color-primary;
					}
				}
			}
		}
	}
</style>
<template>
	<view class="content">
		<view class="top-bar">
			<uni-search-bar @confirm="confirm" @clear="confirm" placeholder="搜索新闻标题" v-model="pagerequest.title" />
			<scroll-view scroll-x="true" >
				<view class="top-menu">
					<view class="menu-item" @click="changeMenu(item)" :class="{active: pagerequest.newsCategoryId == item.id}" v-for="item in cates" :key="item.id">
						{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>
		<uni-list>
			<uni-list-item :ellipsis="2" direction="row" v-for="item in news" :key="item.id" :title="item.title"
				:note="item.creationTime | formatDate" :to="'/pages/news/news-detail?id='+item.id+'&title='+item.title">
				<template v-slot:header>
					<view class="uni-thumb">
						<!-- 当前判断长度只为简单判断类型，实际业务中，根据逻辑直接渲染即可 -->
						<image :src="item.imageUrl | absUrl" mode="aspectFill"></image>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
		<uni-load-more :status="status"></uni-load-more>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cates: [],
				news: [],
				pagerequest: {
					maxResultCount: 20,
					skipCount: 0,
					sorting: 'id desc',
					title:'',
					newsCategoryId: null
				},
				status: 'more'
			};
		},
		onLoad(option) {
			this.getCates().then(res=>{
				this.getpage();
			});
		},
		onReachBottom() {
			this.getpage();
		},
		onPullDownRefresh() {
			this.news = [];
			this.getpage();
		},
		methods: {
			getpage() {
				this.status = 'loading';
				this.pagerequest.skipCount = this.news.length;
				
				this.$http.get("/api/services/app/News/GetAll", {
					params: this.pagerequest
				}).then(res => {
					//console.log(res.data.result.totalCount);
					this.news = this.news.concat(res.data.result.items);
					if (res.data.result.totalCount == this.news.length) {
						this.status = "noMore";
					} else {
						this.status = "more";
					}
					uni.stopPullDownRefresh();
				});
			},
			confirm() {
				this.news = [];
				this.getpage();
			},
			getCates() {
				return this.$http.get("/api/services/app/NewsCategory/GetAllInMenu")
				.then(res=>{
					this.cates = res.data.result;
					this.pagerequest.newsCategoryId = this.cates[0].id;
				});
			},
			changeMenu(cate){
				if (this.pagerequest.newsCategoryId == cate.id) {
					return;
				}
				this.pagerequest.newsCategoryId = cate.id;
				this.news = [];
				this.getpage();
			}
		}
	}
</script>
